//*************************************************************************************
/** \file task_temphumid.cc
 *    This file contains a task which periodically reads a Sensirion SHT15 
 *    temperature and humidity sensor. The sensors can take rather a long time to
 *    read, so it's a good idea to read them only as often as necessary.
 *
 *  States in Task:
 *    \li 0 - Read temperature: Gets temperature data from SHT15 and saves it
 *    \li 1 - Read humidity: Gets humidity data from SHT15 and saves it
 *
 *  Revisions:
 *    \li 12-27-07 JRR Original file
 *    \li 12-30-07 JRR Actually made the thing work and spelled Fahrenheit right
 *    \li 06-01-08 JRR Changed serial stuff to use base_text_serial and descendents
 */
//*************************************************************************************

#include <stdlib.h>
#include <avr/io.h>
#include "avr_1wire.h"
#include "avr_sht15.h"
#include "base_text_serial.h"               // For writing things to serial devices
#include "global_debug.h"
#include "stl_timer.h"
#include "task_temphumid.h"


//-------------------------------------------------------------------------------------
/** This constructor creates a new temperature and humidity reading task. It must be
 *  given a pointer to an object of type avr_sht15 which interfaces to the sensor. 
 *  @param p_sensor A pointer to the avr_sht15 object which interfaces to the sensor
 *  @param a_timer A reference to the task timer
 *  @param interval_time A time stamp containing the time interval between runs
 */

task_temphumid::task_temphumid (avr_sht15* p_sensor, task_timer& a_timer, 
								time_stamp interval_time)
    : stl_task (a_timer, interval_time)
    {
    the_sensor = p_sensor;

    GLOB_DEBUG ("Wind task constructor\r\n");
    }


//-------------------------------------------------------------------------------------
/** This is the function which runs when it is called by the task scheduler. It gets 
 *  readings from the temperature and humidity sensors, one reading each time this
 *  method is called. The readings are spaced out in time to avoid blocking other
 *  tasks. 
 *  FIXME: Sums are added up so that average values can be computed later. 
 *  @param state The state of the task when this run method begins running
 *  @return The state to which the task will transition, or STL_NO_TRANSITION if no
 *      transition is called for at this time
 */

char task_temphumid::run (char state)
    {
    switch (state)
        {
        // If we previously read humidity, read temperature this time
        case (0):
            #ifdef USE_FAHRENHEIT
                current_temp = the_sensor->fahrenheit ();
            #else
                current_temp = the_sensor->celsius ();
            #endif

            return (1);                     // Next time we will read humidity
            break;

        // If we previously read temperature, read humidity this time
        case (1):
            current_humid = the_sensor->humidity ();

            return (0);                     // Next time we will read temperature
            break;

        // If we've entered a state that shouldn't happen, complain and go on
        default:
            GLOB_DEBUG ("WARNING: Temp/humid task in state " << state << endl);
            return (0);
        };
    }

        
//-------------------------------------------------------------------------------------
/** This method returns the most recently read temperature. The temperature is returned
 *  as an unsigned integer holding the number of tenths of a degree Celsius or 
 *  Fahrenheit, depending on whether USE_FAHRENHEIT has been defined. 
 *  @return The temperature which was most recently measured by the SHT15 sensor
 */

unsigned int task_temphumid::get_latest_temperature (void)
    {
    return (current_temp);
    }


//-------------------------------------------------------------------------------------
/** This method returns the most recently read humidity. The humidity is returned as 
 *  an integer, scaled in percent relative humidity. 
 *  @return The humidity which was most recently read from the SHT15 sensor
 */

unsigned char task_temphumid::get_latest_humidity (void)
    {
    return (current_humid);
    }
